package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.ClubPopularListAdapter;
import com.example.administrator.yuejian_2.bean.ClubListBean;
import com.example.administrator.yuejian_2.fragment.ClubFragment;
import com.example.administrator.yuejian_2.interservice.ClubService;
import com.example.administrator.yuejian_2.interservice.Const;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ClubSearchActivity extends AppCompatActivity {
    private EditText club_name;
    private ImageButton back;
    private Button search_button;
    private RecyclerView recyclerView;
    private ClubPopularListAdapter adapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_search);
        club_name = (EditText) findViewById(R.id.club_name);
        recyclerView = (RecyclerView) findViewById(R.id.club_all_recyclerView);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                finish();
                intent.setClass(ClubSearchActivity.this, ClubFragment.class);
            }
        });
        search_button = (Button) findViewById(R.id.search_button);
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               searchClub(club_name.getText().toString());
            }
        });
    }
    public void searchClub(String name){
        context = ClubSearchActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<List<ClubListBean>> call = service.search(name);
            call.enqueue(new Callback<List<ClubListBean>>() {
                @Override
                public void onResponse(Call<List<ClubListBean>> call, Response<List<ClubListBean>> response) {
                    if (response.body()!= null){
                        adapter = new ClubPopularListAdapter(context, response.body());
                        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(layoutManager);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());
                    }
                }

                @Override
                public void onFailure(Call<List<ClubListBean>> call, Throwable t) {

                }
            });


    }
}
